
/**
 * Module dependencies.
 */

var express = require('express'), 
      fs = require('fs');
      routes = require('./routes');

var app = module.exports = express.createServer();

// Configuration




// Routes

//app.get('/', routes.index);


app.use(express.bodyParser({uploadDir:'./files'}));

app.get('/zdjecia', function(req, res){
  var kod;
  
  var fs = require('fs');
  fs.readdir('./files/', function (err, files) { if (err) throw err;
      //console.log(files);
      for (file in files){
      kod = kod + '<img src="files/' + files[file] + '"/><br/>';
      };
      res.send(kod);
  });

});



app.get('/upload', function(req, res){

res.send('<form method="post" enctype="multipart/form-data" action="/file-upload"><input type="file" name="thumbnail"><input type="submit"></form>');
});

app.post('/file-upload', function(req, res) {
    // get the temporary location of the file
    var tmp_path = req.files.thumbnail.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = './public/images/' + req.files.thumbnail.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
        });
    });
});


app.listen(3000, function(){
  console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
